import java.util.ArrayList;
import java.util.Arrays;

public class JugNode extends Node
{
    //Constants to represent the various positions
    private static final int j1value = 0;
    private static final int j2value = 1;

    
    JugNode(State state, int depth, double g, double h, Node parent)
    {
        super(state,depth,g,h,parent);
    }
    
    public  ArrayList<Node> getSuccessors()
    {
        ArrayList<Node> succs = new ArrayList<Node>(10);                 
        int [] temp = convertStateToArray(getState());
        
        //Run the five actions and make sure the states are possible:
        //System.out.println("Parent: "+ Arrays.toString(temp));
        movexMyJ(temp,succs);
        
        if (succs.size()==0)
            return null;
        else
            return succs;   
    }
    
    private static int[] convertStateToArray(State current)
    {
        String value = current.getValue();
        int[] jb = new int[2];
        for (int i=0; i<jb.length; i++)
            jb[i] = Character.digit(value.charAt(i), 10);
        return jb;
    }
    
    private static String convertArrayToString(int [] jb)
    {
        StringBuffer sb = new StringBuffer();
        for(int i:jb)
            sb.append(String.valueOf(i));
        return sb.toString();
    }
    
    private void movexMyJ(int[] curr,ArrayList<Node> succs)
    {
        int j1 = curr[j1value];
        int j2 = curr[j2value];
        
        if(j1+j2<5){
        	int[] copy = Arrays.copyOf(curr,2);
        	copy[0]= 0;
        	copy[1] = j1+j2;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }else{
        	int[] copy = Arrays.copyOf(curr,2);
        	System.out.println(((j1+j2)%4));
        	copy[0]= (j1+j2)%4;
        	copy[1] = 4;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }
        
        if((j2+j1)<4){
        	int[] copy = Arrays.copyOf(curr,2);
        	copy[0]= j2+j1;
        	copy[1] = 0;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }else{
        	int[] copy = Arrays.copyOf(curr,2);
        	System.out.println(((j1+j2)%4));
        	copy[0]= 3;
        	copy[1] = (j1+j2)%3;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }
        
        if(j1 != 3){
        	int[] copy = Arrays.copyOf(curr,2);
        	copy[0]= 3;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }
        
        if(j2 != 4){
        	int[] copy = Arrays.copyOf(curr,2);
        	copy[1]= 4;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }
        
        if(j2 != 0){
        	int[] copy = Arrays.copyOf(curr,2);
        	copy[1] = 0;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }
        
        if(j1 != 0){
        	int[] copy = Arrays.copyOf(curr,2);
        	copy[0] = 0;
        	State temp = new State(convertArrayToString(copy));
            //System.out.println("Adding: "+ temp.toString());
            succs.add(new JugNode(temp, depth+1,0,0,this));
        }
    }
}

